package com.slaffka.action;

import com.slaffka.dao.FilialDAO;
import com.slaffka.dao.UserDAO;
import com.slaffka.model.Filial;
import com.slaffka.model.User;
import org.jboss.seam.security.Credentials;
import org.jboss.solder.logging.Logger;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: slaffka
 * Date: 26.11.12
 * Time: 21:59
 * To change this template use File | Settings | File Templates.
 */
@SessionScoped
@Named(value = "userSetting")
public class UserSetting implements Serializable {

    @Inject
    UserDAO userDAO;

    @Inject
    FilialDAO filialDAO;

    @Inject
    private Logger log;

    @Inject
    private Credentials credentials;

    private Filial currentFilial;

    public void setCurrentFilial(Filial currentFilial) {
        this.currentFilial = currentFilial;

    }

    public User getCurrentUser(){
        String username = credentials.getUsername();
        return userDAO.getUserByUsername(username);
    }

    public Filial getCurrentFilial(){
        User currentUser = getCurrentUser();
        Filial filial = currentUser.getFilial();
        if (filial == null){
            filial = currentFilial;
        }
        return filial;
    }
}
